//
// Created by AnKun on 2019/12/3.
//

#ifndef MYHASHMAP_HASHMAP_H
#define MYHASHMAP_HASHMAP_H

#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <stdbool.h>
#include "malloc.h"
#include "List.h"

#define hmalloc mem_malloc
#define hfree   mem_free
#define hmemcpy mem_memcpy
#define hmemset mem_memset
#define hrealloc mem_realloc


#define KEY_MAX_LEN 16      // key的最大长度
#define VAL_MAX_LEN 32      // value的最大长度

#pragma pack(4)

typedef struct list_head HashTable;

typedef struct
{
    HashTable *items;   // 键值对条目，如果有冲突键值对则挂在链表后
    uint32_t used;      // 使用量
    uint32_t size;      // 总大小
} HashMap;

typedef struct
{
    uint8_t key[KEY_MAX_LEN];
    uint8_t value[VAL_MAX_LEN];
    struct list_head node;
} Entry;

HashMap *HashMap_Create(uint32_t size);
void HashMap_Destroy(HashMap *hashMap);
bool HashMap_Put(HashMap *hashMap, const void *key, const void *value);
void *HashMap_Get(const HashMap *const hashMap, const void *key);
void HashMap_Clear(HashMap *hashMap);
void HashMap_Remove(HashMap *hashMap, const void *key);
bool HashMap_Exists(const HashMap *const hashMap, const void *key);
void HashMap_GetKeys(const HashMap *const hashMap, uint8_t **keys, uint32_t *count);

#pragma pack()

#endif //MYHASHMAP_HASHMAP_H
